package hottargui.domain.beta;
import hottargui.domain.*;

public class BetaWinnerStrategyImpl implements WinnerStrategy {

	private Board _board;
	private Tile _saltMineTile;


	public BetaWinnerStrategyImpl(Board b) {
		_board = b;
		_saltMineTile = _board.getTile(new Position(3,3));
	}

	public PlayerColor getTheWinner(){
		return getWinnerSecundaryObjectiv();
	}

	public PlayerColor[] getWinnerPrimaryObjectiv(){
		PlayerColor[] _primaryWinners = new PlayerColor[4];
		int i = 0;

		int r_red = _board.getRevenue(PlayerColor.Red);
		int r_blue = _board.getRevenue(PlayerColor.Blue);
		int r_green = _board.getRevenue(PlayerColor.Green);
		int r_yellow = _board.getRevenue(PlayerColor.Yellow);



		// add winner on primary objective to the winner array 
		if ((r_red >= r_blue) && (r_red >= r_green) && (r_red >= r_yellow) ) {
			_primaryWinners[i] = PlayerColor.Red;
			i ++;
		}

		if ((r_blue >= r_red) && (r_blue >= r_green) && (r_blue >= r_yellow)) {
			_primaryWinners[i] = PlayerColor.Blue;
			i++;
		}

		if ((r_green >= r_red) && (r_green >= r_blue) && (r_green >= r_yellow)) {
			_primaryWinners[i] = PlayerColor.Green;
			i++;
		}

		if ((r_yellow >= r_red) && (r_yellow >= r_blue) && (r_yellow >= r_green)) {
			_primaryWinners[i] = PlayerColor.Yellow;
			i++;
		}
		return _primaryWinners;
	}


	public PlayerColor getWinnerSecundaryObjectiv() {
		PlayerColor[] _primaryWinner = getWinnerPrimaryObjectiv();
		//no Winner found i primary objective
		if (_primaryWinner[0] == null){
			return PlayerColor.None;
		}  
		//only one winner found i primary objective
		if ((_primaryWinner[0] != null) && (_primaryWinner[1] == null)){
			return _primaryWinner[0];
		}
		//more winners found i primary objective
		for (int i = 0; i <4;i++){
			if (_primaryWinner[i] == _saltMineTile.getOwnerColor()){
				return (_primaryWinner[i]);
			}
		}
		//unable to determine a winner
		return PlayerColor.None;
	}
}
